package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PropertyAccount;
import com.ruoyi.system.domain.PropertyContract;
import com.ruoyi.system.mapper.PropertyAccountMapper;
import com.ruoyi.system.service.IPropertyAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class PropertyAccountServiceImpl implements IPropertyAccountService {

    @Autowired
    private PropertyAccountMapper propertyAccountMapper;

    @Override
    public List<PropertyAccount> selectPropertyAccountList(PropertyAccount propertyAccount) {
        return propertyAccountMapper.selectPropertyAccountList(propertyAccount);
    }

    @Override
    public int insertPropertyAccount(PropertyAccount propertyAccount) {
        String accountId = UUID.randomUUID().toString().replaceAll("-", "");
        propertyAccount.setAccountId(accountId);
        return propertyAccountMapper.insertPropertyAccount(propertyAccount);
    }

    @Override
    public PropertyAccount getPropertyAccountById(String accountId) {
        return propertyAccountMapper.getPropertyAccountById(accountId);
    }

    @Override
    public int updatePropertyAccount(PropertyAccount propertyAccount) {
        return propertyAccountMapper.updatePropertyAccount(propertyAccount);
    }

    @Override
    public int removePropertyAccount(String ids) {
        return propertyAccountMapper.removePropertyAccount(Convert.toStrArray(ids));
    }

    /*校验导入的Excel数据*/
    @Override
    public String importPropertyAccount(List<PropertyAccount> paList) {
        int errorData = 0;
        int updateCount = 0;
        int insertCount = 0;
        StringBuilder errorDatas = new StringBuilder();
        for(PropertyAccount pa:paList) {
            if(null==pa.getNum()){
                errorData++;
                String errStr = "台账" + pa.getFileContent() + "序号为空；</br>";
                errorDatas.append(errStr);
                continue;
            }
            if(StringUtils.isNotEmpty(pa.getAccountId())){  //update
                try{
                    PropertyAccount propertyAccount =  propertyAccountMapper.getPropertyAccountById(pa.getAccountId());
                    if (propertyAccount!=null){
                        propertyAccountMapper.updatePropertyAccount(pa);
                    }else{
                        propertyAccountMapper.insertPropertyAccount(pa);
                    }
                    updateCount++;
                }catch (Exception e){
                    e.printStackTrace();
                    errorData++;
                    String errStr = "台账" + pa.getFileContent() + "修改失败；"+e.getMessage()+"</br>";
                    errorDatas.append(errStr);
                    continue;
                }
            }else{  //insert
                try{
                    pa.setAccountId(UUID.randomUUID().toString().replaceAll("-",""));
                    propertyAccountMapper.insertPropertyAccount(pa);
                    insertCount++;
                }catch (Exception e){
                    e.printStackTrace();
                    errorData++;
                    String errStr = "台账" + pa.getFileContent() + "新增失败；"+e.getMessage()+"</br>";
                    errorDatas.append(errStr);
                    continue;
                }
            }
        }
        String message;
        if(paList.size()>0){
            message = "共导入"+paList.size()+"条数据。</br>";
        }else{
            return "无数据";
        }
        message = message + "成功新增" + insertCount + "条数据，成功更新"+updateCount+"条数据</br>";
        if(StringUtils.isNotEmpty(errorDatas.toString())){
            message = message + "其中" + errorData + "条数据导入失败，错误原因如下：</br>";
            message = message + errorDatas.toString();
        }
        return message;
    }
}
